<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>更新到 0.7 - The Rust Rand Book</title>
        
        


        <!-- Custom HTML head -->
        


        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="Extended documentation for Rust&#x27;s Rand lib">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        
        <link rel="icon" href="favicon.svg">
        
        
        <link rel="shortcut icon" href="favicon.png">
        
        <link rel="stylesheet" href="css/variables.css">
        <link rel="stylesheet" href="css/general.css">
        <link rel="stylesheet" href="css/chrome.css">
        <link rel="stylesheet" href="css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
        
        <link rel="stylesheet" href="fonts/fonts.css">
        

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="highlight.css">
        <link rel="stylesheet" href="tomorrow-night.css">
        <link rel="stylesheet" href="ayu-highlight.css">

        <!-- Custom theme stylesheets -->
        

        
    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><a href="intro.html">介绍</a></li><li class="chapter-item expanded "><a href="guide-start.html"><strong aria-hidden="true">1.</strong> 入门指南</a></li><li class="chapter-item expanded "><a href="crates.html"><strong aria-hidden="true">2.</strong> crate 和特性</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="crates-rand.html"><strong aria-hidden="true">2.1.</strong> 随机加密(co)</a></li><li class="chapter-item expanded "><a href="crates-gen.html"><strong aria-hidden="true">2.2.</strong> 随机信号生成器</a></li></ol></li><li class="chapter-item expanded "><a href="guide.html"><strong aria-hidden="true">3.</strong> 指南</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="guide-data.html"><strong aria-hidden="true">3.1.</strong> 随机数据</a></li><li class="chapter-item expanded "><a href="guide-gen.html"><strong aria-hidden="true">3.2.</strong> 生成器类型</a></li><li class="chapter-item expanded "><a href="guide-rngs.html"><strong aria-hidden="true">3.3.</strong> 随机数生成器(RNGs)</a></li><li class="chapter-item expanded "><a href="guide-seeding.html"><strong aria-hidden="true">3.4.</strong> 为随机数生成器置种</a></li><li class="chapter-item expanded "><a href="guide-values.html"><strong aria-hidden="true">3.5.</strong> 随机值</a></li><li class="chapter-item expanded "><a href="guide-dist.html"><strong aria-hidden="true">3.6.</strong> 各种分布方式</a></li><li class="chapter-item expanded "><a href="guide-seq.html"><strong aria-hidden="true">3.7.</strong> 序列</a></li><li class="chapter-item expanded "><a href="guide-err.html"><strong aria-hidden="true">3.8.</strong> 错误处理</a></li></ol></li><li class="chapter-item expanded "><a href="portability.html"><strong aria-hidden="true">4.</strong> 可移植性</a></li><li class="chapter-item expanded "><a href="update.html"><strong aria-hidden="true">5.</strong> 更新</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="update-0.5.html"><strong aria-hidden="true">5.1.</strong> 更新到 0.5</a></li><li class="chapter-item expanded "><a href="update-0.6.html"><strong aria-hidden="true">5.2.</strong> 更新到 0.6</a></li><li class="chapter-item expanded "><a href="update-0.7.html" class="active"><strong aria-hidden="true">5.3.</strong> 更新到 0.7</a></li></ol></li><li class="chapter-item expanded "><a href="contributing.html"><strong aria-hidden="true">6.</strong> 贡献</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="contrib-doc.html"><strong aria-hidden="true">6.1.</strong> 文档</a></li><li class="chapter-item expanded "><a href="contrib-scope.html"><strong aria-hidden="true">6.2.</strong> Scope</a></li><li class="chapter-item expanded "><a href="contrib-test.html"><strong aria-hidden="true">6.3.</strong> 测试</a></li><li class="chapter-item expanded "><a href="contrib-bench.html"><strong aria-hidden="true">6.4.</strong> 基准</a></li></ol></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                        
                    </div>

                    <h1 class="menu-title">The Rust Rand Book</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                        
                    </div>
                </div>

                
                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <blockquote>
<p><a href="https://github.com/rust-random/book/blob/master/src/update-0.7.md">update-0.7.md</a><br />
commit: 1552084912cc773ba6e528d216b0a351fe0468b4 <br />
本章译文最后维护日期：2020-12-1</p>
</blockquote>
<h1><a class="header" href="#updating-to-07" id="updating-to-07">Updating to 0.7</a></h1>
<h1><a class="header" href="#更新至07版" id="更新至07版">更新至0.7版</a></h1>
<p>自0.6版本发布以来，<a href="https://github.com/rust-random">rust-random</a> 获得了一个 logo 和一个新的 crate：<a href="https://github.com/rust-random/getrandom">getrandom</a>！</p>
<h2><a class="header" href="#dependencies" id="dependencies">Dependencies</a></h2>
<h2><a class="header" href="#依赖" id="依赖">依赖</a></h2>
<p>Rand crates 现在需要 <code>rustc</code> 1.32.0或更的高版本。
这样我们就可以移除所有的 <code>build.rs</code>文件以加快编译速度。</p>
<p>Rand crate 现在整体上依赖更少了，尽管有一些新的。</p>
<h2><a class="header" href="#getrandom" id="getrandom">Getrandom</a></h2>
<p>如前所述，我们有了一个新的 crate：<a href="https://github.com/rust-random/getrandom">getrandom</a>，它提供了一个最小的API，可以独立于平台访问新鲜熵源。这替换了 [<code>OsRng</code>] 中以前的实现，现在它只是一个包装器。</p>
<h2><a class="header" href="#core-features" id="core-features">Core features</a></h2>
<h2><a class="header" href="#核心特性" id="核心特性">核心特性</a></h2>
<p><a href="https://docs.rs/rand/0.6.0/rand/trait.FromEntropy.html"><code>FromEntropy</code></a> trait 现在已被移除。不过不用担心，它的 <a href="https://rust-random.github.io/rand/rand/trait.SeedableRng.html#method.from_entropy"><code>from_entropy</code></a>方法在其“新家” <a href="https://rust-random.github.io/rand/rand/trait.SeedableRng.html"><code>SeedableRng</code></a> trait 中继续能对外提供简单的初始化功能（这需要 <code>rand_core</code> 启用 <code>std</code>特性或 <code>getrandom</code>特性）：</p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use rand::{SeedableRng, rngs::StdRng};
let mut rng = StdRng::from_entropy();
<span class="boring">}
</span></code></pre></pre>
<p><a href="https://rust-random.github.io/rand/rand/trait.SeedableRng.html#method.from_rng"><code>SeedableRng::from_rng</code></a>方法现在被认为是值稳定的：实现返回的结果应该可移植。<!-- tobocheck --></p>
<p><code>rand_core</code> 和 <code>rand</code> 的 <a href="https://rust-random.github.io/rand/rand_core/struct.Error.html"><code>Error</code></a>类型的进行了重大的重新设计；直接使用这种类型可能需要做出一些调整。</p>
<h2><a class="header" href="#prngs" id="prngs">PRNGs</a></h2>
<p>与上一版本相比，这些版本的更改较少，但值得注意的是：</p>
<ul>
<li><a href="https://crates.io/crates/rand_chacha"><code>rand_chacha</code></a> 已被重写以获得更好的性能（通过SIMD指令）。</li>
<li><a href="https://rust-random.github.io/rand/rand/rngs/struct.StdRng.html"><code>StdRng</code></a> 和 <a href="https://rust-random.github.io/rand/rand/rngs/struct.ThreadRng.html"><code>ThreadRng</code></a> 现在使用了 ChaCha算法。这是 <a href="https://rust-random.github.io/rand/rand/rngs/struct.StdRng.html"><code>StdRng</code></a> 的破坏性变更(value-breaking change)。</li>
<li><a href="https://rust-random.github.io/rand/rand/rngs/struct.SmallRng.html"><code>SmallRng</code></a> 现在在 <code>small_rng</code>特性标志位后面被选通。</li>
<li><a href="https://rust-random.github.io/rand/rand/rngs/struct.SmallRng.html"><code>SmallRng</code></a> is now gated behind the <code>small_rng</code> feature flag.</li>
<li><code>xoshiro</code> crate 现在位于 <a href="https://crates.io/crates/rand_xoshiro"><code>rand_xoshiro</code></a> 下。</li>
<li><code>rand_pcg</code> 现在包含了 <a href="https://rust-random.github.io/rand/rand_pcg/type.Pcg64.html"><code>Pcg64</code></a>.</li>
</ul>
<h2><a class="header" href="#distributions" id="distributions">Distributions</a></h2>
<h2><a class="header" href="#分布" id="分布">分布</a></h2>
<p>对于最广泛使用的分布（<a href="https://rust-random.github.io/rand/rand/distributions/struct.Standard.html"><code>Standard</code></a> 和 <a href="https://rust-random.github.io/rand/rand/distributions/struct.Uniform.html"><code>Uniform</code></a>），此版本没有明显的变化。但对于其他的<em>大部分</em>来说...</p>
<ul>
<li>我们添加了一个新的 crate [rand_distr]，用于存放所有分布（包括重新导出仍在 <a href="https://rust-random.github.io/rand/rand/distributions/index.html"><code>rand::distributions</code></a> 中的分布）。如果以前使用的是 <a href="https://rust-random.github.io/rand/rand/distributions/struct.Normal.html"><code>rand::distributions::Normal</code></a>，那么现在应该使用 <a href="https://rust-random.github.io/rand/rand_distr/struct.Normal.html"><code>rand_distr::Normal</code></a>。</li>
<li>许多分布的构造函数都进行了更改，以便于返回 <code>Result</code>，而不是碰到出错就直接 panic 了事。</li>
<li>现在许多分布在其参数上已经实现了泛型化（在大多数情况下支持 <code>f32</code> 和 <code>f64</code>）。这有助于使用泛型代码，并可以减小那些参数化的分布的尺寸。目前比较复杂的算法总是在内部使用 <code>f64</code>。</li>
<li><a href="https://rust-random.github.io/rand/rand/distributions/struct.Standard.html"><code>Standard</code></a> 现在可以抽样 <a href="https://doc.rust-lang.org/std/num/index.html"><code>NonZeroU*</code></a> 数字了。</li>
</ul>
<p>我们新增了几个分布：</p>
<ul>
<li><a href="https://rust-random.github.io/rand/rand/distributions/alias_method/struct.WeightedIndex.html"><code>rand::distributions::weighted::alias_method::WeightedIndex</code></a></li>
<li><a href="https://rust-random.github.io/rand/rand_distr/struct.Pert.html"><code>rand_distr::Pert</code></a></li>
<li><a href="https://rust-random.github.io/rand/rand_distr/struct.Triangular.html"><code>rand_distr::Triangular</code></a></li>
<li><a href="https://rust-random.github.io/rand/rand_distr/struct.UnitBall.html"><code>rand_distr::UnitBall</code></a></li>
<li><a href="https://rust-random.github.io/rand/rand_distr/struct.UnitDisc.html"><code>rand_distr::UnitDisc</code></a></li>
<li><a href="https://rust-random.github.io/rand/rand_distr/struct.UnitSphere.html"><code>rand_distr::UnitSphere</code></a>（之前被称为 <code>rand::distributions::UnitSphereSurface</code>）</li>
</ul>
<h2><a class="header" href="#sequences" id="sequences">Sequences</a></h2>
<h2><a class="header" href="#序列" id="序列">序列</a></h2>
<p>为了便于移植，当上限小于 <code>u32::MAX</code> 时，所有类型为 <code>usize</code> 的随机样本都将在内部被转换为对 <code>u32</code>类型的值进行抽样。这意味着升级到0.7版对于使用<code>seq</code>函数来说是一个破坏性变更，但是在升级到0.7版之后，抽样结果应该在跨CPU架构中保持一致。</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                        
                            <a rel="prev" href="update-0.6.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                        

                        
                            <a rel="next" href="contributing.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                
                    <a rel="prev" href="update-0.6.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                

                
                    <a rel="next" href="contributing.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
                
            </nav>

        </div>

        

        

        

        
        <script type="text/javascript">
            window.playground_copyable = true;
        </script>
        

        

        
        <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
        

        <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
        

        

    </body>
</html>
